package cn.topcodes.tcsf.app.web.controller.admin;

import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.app.domain.form.AppAboutSettings;
import cn.topcodes.tcsf.app.service.impl.AppSettingsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @author Unicorn
 * @date 2017/5/5
 */
@Controller
@RequestMapping("/admin/app-settings")
public class AppSettingsController {

    @Autowired
    private AppSettingsServiceImpl appSettingsService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index(Model model) {
        AppAboutSettings appAboutSettings = appSettingsService.getAppAboutSettings();
        model.addAttribute("appAboutSettings", appAboutSettings);
        return "app/admin/app-settings";
    }

    @ResponseBody
    @RequestMapping(value = "save-app-about-settings", method = RequestMethod.POST)
    public ApiResponseEntity saveAppAboutSettings(
            @Valid AppAboutSettings appAboutSettings,
            BindingResult result
            ) {
        ValidationUtil.validate(result);
        appSettingsService.setAppAboutSettings(appAboutSettings);
        return ApiResponseEntity.buildOK();
    }
}
